#!/usr/bin/env bash

test -z "${VERBOSE:-}${verbose:-}" || set -x

emoji_continue=$(echo -e "\U1F449")

if [ -z "${text}" ]
then
  read -r message <<EOF
{"msg_type":"text","content":{"text":"${status:-} ${CI_PROJECT_NAMESPACE}/${CI_PROJECT_NAME}(${CI_JOB_STARTED_AT})\r\n${emoji_continue} ${CI_PROJECT_URL} > ${CI_BUILD_REF_NAME} > ${CI_PIPELINE_ID} (${CI_COMMIT_AUTHOR})\r\n${emoji_continue} ${CI_PROJECT_URL}/-/commit/${CI_COMMIT_SHA} ${CI_COMMIT_TITLE}\r\n${emoji_continue} ${CI_PROJECT_URL}/-/pipelines/${CI_PIPELINE_ID}\r\n${emoji_continue} ${extra:-eof}"}}
EOF
else
  read -r message <<EOF
{"msg_type":"text","content":{"text":"${text}"}}
EOF
fi

for id in "$@"
do
  curl -X POST -H 'content-type:application/json' -vskS "https://open.feishu.cn/open-apis/bot/v2/hook/${id}" \
    -d "${message}" 2>/dev/null
done
